package elephant.job;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import elephant.Container;
/**
 * 
 * @author icecooly
 *
 */
public abstract class BaseJob implements Runnable{
	//
	private static Logger logger=LoggerFactory.getLogger(BaseJob.class);
	//
	protected String id;
	protected String cronExpression;//eg:"0 30 0 * * ?"
	private Date lastRunTime;
	//
	public Date getNextRunDate(){
		try {
			CronExpression cron=new CronExpression(cronExpression);
			if(lastRunTime==null){
				return cron.getNextValidTimeAfter(Container.get().getStartTime());
			}
			return cron.getNextValidTimeAfter(lastRunTime);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}
	//
	public void runJob(){
		lastRunTime=new Date();
		Container.get().executeThreadWorker(this);
	}
	//
	public String getCronExpression() {
		return cronExpression;
	}
	public Date getLastRunTime() {
		return lastRunTime;
	}
	public String getId() {
		return id;
	}
}
